<?php

namespace app\agentapi\controller\order;

use app\agentapi\controller\Controller;
use think\Cache;
use think\Db;
use Predis\Client;
/**
 * 楼盘相关
 */
class Billds extends Controller{
    public $server;
    public $redis;
    public function __construct(){
        $this->server=[
            'host' => '127.0.0.1',
            'port' => 6379,
            ];
        $this->redis= new Client($this->server);
    }
    /**
     * redis  写入数据
     */
    public function addredis(){
        $count=input('post.count');
        $ret = $this->redis->hMSet('product_info_'.$count, array(
            'product_id'            => $count,
            'product_name'          => $count.'Gregory登山包B75蓝色2015款',
            'product_price'         => $count,
            'product_sales_volume'  => $count,
            'product_stocks'        => $count,
        ));
        if($ret){
            return json(['msg'=>'设置成功','code'=>'success','data'=>$ret]);
        }
        return json(['msg'=>'设置失败','code'=>'error','data'=>$ret]);
    }
    /**
     * redis  读数据
     */
    public function getRedis(){
        $count=input('post.count');
        $ret = $this->redis->hGetAll('product_info_'.$count);
        if($ret){
            return json(['msg'=>'获取成功','code'=>'success','data'=>$ret]);
        }
        return json(['msg'=>'获取失败','code'=>'error','data'=>$ret]);
    }
    /**
     * redis  修改数据
     */
    public function updateredis(){
        $count=input('post.count');
        $ret = $this->redis->hMSet('product_info_'.$count, array(
            'product_id'            => 'a',
            'product_name'          => $count.'Gregory登山包B75蓝色2015款',
            'product_price'         => 'a',
            'product_sales_volume'  => 'a',
            'product_stocks'        => 'a',
        ));
        if($ret){
            return json(['msg'=>'修改成功','code'=>'success','data'=>$ret]);
        }
        return json(['msg'=>'修改失败','code'=>'error','data'=>$ret]);
    }
    /**
     * redis 删除数据
     */
    public function delredis(){
        $count=input('post.count');
        $ret = $this->redis->hDel('product_info_'.$count,'product_id','product_name','product_price','product_sales_volume','product_stocks');
        if($ret){
            return json(['msg'=>'删除成功','code'=>'success','data'=>$ret]);
        }
        return json(['msg'=>'删除失败','code'=>'error','data'=>$ret]);
    }
    /**
     * 多条  获取
     */
    public function getall(){
        $ret=$this->redis->hgetall('zxm');
        return json(['msg'=>'获取成功','code'=>'success','data'=>$ret]);
    }
    /**
     * 多条  写入
     */
    public function insertall(){
        $this->redis->hmset('zxm',['1'=>json_encode(['a'=>1,'b'=>2,'c'=>3]),'2'=>json_encode(['a'=>4,'b'=>5,'c'=>6]),'3'=>json_encode(['a'=>7,'b'=>8,'c'=>9])]);
        return json(['msg'=>'写入成功','code'=>'success','data'=>[]]);
    }

    /**
     * redis  排序
     */
    public function rank(){
        $dataOne = [];
        for($i=0;$i<5;$i++){
            //生成随机数
            $num=rand(0,100);
            //生成随机字符串
            $str=$this->get_random(6,'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ');
            $this->redis->zAdd('paixu1',$num,json_encode(['name'=>$str]));
            //由大到小排序
            $dataOne=$this->redis->zRevRange('paixu1',0,-1,[]);
            //由小到大排序
            $dataTow=$this->redis->ZRANGE('paixu1',0,-1,[]);
        }

        return json(['msg'=>'测试排序','升序'=>$dataTow,'降序'=>$dataOne]);
    }

    /**
     * 生成随机字符串
     */
    public function get_random($len,$chars){
        $hash="";
        $max=strlen($chars)-1;
        for($i=0;$i<$len;$i++){
            $hash.=$chars[mt_rand(0,$max)];
        }
        return $hash;
    }
}
